package asteroids.statement;

import asteroids.*;
import asteroids.model.programs.*;
import asteroids.expression.*;

public class AssignStatement extends Statement {

	private Expression exp;
	private String name;

	public AssignStatement(String newName, Expression e) {
		
		if (e == null) {
			this.exp = new EntityExpression();
		} else {
			this.exp = e;
		}
		if (newName == null) {
			this.name = "";
		} else {
			this.name = newName;
		}
	}

	@Override
	public void execute(Program program) {
		Expression localExp = exp;
		if (exp instanceof Variable) {
			localExp = ((Variable) exp).getRef(program);
		}
		if (localExp instanceof NumericExpression) {
			double value = ((NumericExpression) localExp).getValue(program);
			localExp = new NumericExpression();
			((NumericExpression) localExp).setValue(value);

		} else if (localExp instanceof BooleanExpression) {
			boolean value = ((BooleanExpression) localExp).getValue(program);
			localExp = new BooleanExpression();
			((BooleanExpression) localExp).setValue(value);

		} else if (localExp instanceof EntityExpression) {
			FlyingObject value = ((EntityExpression) localExp)
					.getValue(program);
			localExp = new EntityExpression(value);
		}

		program.setGlobals(name, localExp);
		finished = true;

	}

	@Override
	public int getSize() {
		return 1;
	}

}
